//
//  MainController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/1.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "MainController.h"
#import <JhtMarquee/JhtVerticalMarquee.h>
#import "OrderViewController.h"
#import "PickUpViewController.h"

@interface MainController ()<UITableViewDelegate,UITableViewDataSource,NewPagedFlowViewDelegate, NewPagedFlowViewDataSource,UIGestureRecognizerDelegate>
{
    // 纵向 跑马灯
    JhtVerticalMarquee *_verticalMarquee;
    // 是否暂停了纵向 跑马灯
    BOOL _isPauseV;
}

@property (nonatomic, strong) UIView *bgBannerView;//轮播背景
@property (nonatomic, strong) NewPagedFlowView *pageFlowView;//卡片轮播图
@property (nonatomic, strong) NSMutableArray *bannerImageArray;//轮播图数组

@property (nonatomic, strong) UIView *bgNoticeView;//公告背景
@property (nonatomic, strong) NSMutableArray *noticesArray;

@property (nonatomic, strong) UIView *sellLookBgView;//销售看板背景
@property (nonatomic, strong) NSMutableDictionary *salesBoard;//销售看板数据
@property (nonatomic, strong) NSMutableDictionary *dayStatistics;//日销售看板数据
@property (nonatomic, strong) NSMutableDictionary *monthStatistics;//月销售看板数据
@property (nonatomic, strong) NSMutableDictionary *weekStatistics;//周销售看板数据
@property (nonatomic, copy) NSString *currentCapital;//当前余额


@property (nonatomic, strong) UIView *backlogBgView;//待办事项模板
@property (nonatomic, strong) NSMutableArray *todoList;//待办事项数据

@property (nonatomic, strong) UIView *recommendBgView;//推荐模板

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *tableViewArray;




@property (nonatomic, strong) UIView *bgStrategyView;//攻略背景
@property (nonatomic, strong) UIImageView *bgStrategyImage;//攻略背景图片
@property (nonatomic, strong) UILabel *numberLabel;//完成数量
@property (nonatomic, strong) PickUpViewController *pickVC;
@end

@implementation MainController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 关闭跑马灯
    [_verticalMarquee marqueeOfSettingWithState:MarqueeContinue_V];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.bgNavView.hidden = YES;
    [self createTableView];
    [self httpRequestData];
}

- (void)httpRequestData
{
    self.noticesArray = [NSMutableArray array];
    self.tableViewArray = [NSMutableArray array];
    self.bannerImageArray = [NSMutableArray array];
    self.todoList = [NSMutableArray array];
    self.salesBoard = [NSMutableDictionary dictionary];
    self.dayStatistics = [NSMutableDictionary dictionary];
    self.weekStatistics = [NSMutableDictionary dictionary];
    self.monthStatistics = [NSMutableDictionary dictionary];
    
    __weak MainController *myself = self;
    [HttpRequest formPostPath:MainIndex_API params:nil resultBlock:^(NSDictionary *Data, MsgModel *msgModel)
     {
         if (msgModel.status == 200)
         {
             NSDictionary *result = Data[@"result"];
             NSLog(@"result--%@",result);
             
             NSArray *banners = result[@"banners"];
             NSArray *notices = result[@"notices"];
             NSArray *recommendItems = result[@"recommendItems"];
             
             self.salesBoard = result[@"salesBoard"];
             self.todoList = result[@"todoList"];
             NSLog(@"salesBoard--%@",self.salesBoard);
             NSLog(@"todoList--%@",self.todoList);
             self.currentCapital = self.salesBoard[@"currentCapital"];
             self.dayStatistics = self.salesBoard[@"dayStatistics"];
             self.weekStatistics = self.salesBoard[@"weekStatistics"];
             self.monthStatistics = self.salesBoard[@"monthStatistics"];
             
             NSError *error;
             //1,banner
             for (NSDictionary *dic in banners)
             {
                 BannersModel *model = [[BannersModel alloc] initWithDictionary:dic error:&error];
                 [myself.bannerImageArray addObject:model];
             }
             dispatch_async(dispatch_get_main_queue(), ^{
                 [myself addBanner];
             });
             
             //2,notices
             for (NSDictionary *dic in notices)
             {
                 NoticesModel *model = [[NoticesModel alloc] initWithDictionary:dic error:&error];
                 [myself.noticesArray addObject:model];
             }
             dispatch_async(dispatch_get_main_queue(), ^{
                 [myself addNotices];
             });
             
             //3,cell数据
             for (NSDictionary *dic in recommendItems)
             {
                 MainModel *model = [[MainModel alloc] initWithDictionary:dic error:&error];
                 [myself.tableViewArray addObject:model];
             }
             
             //5,停止刷新
             [myself.myTableView.mj_header endRefreshing];
             myself.myTableView.tableHeaderView = [self createHeaderView];
             [myself.myTableView reloadData];
             
         }else {
             [myself.myTableView.mj_header endRefreshing];
         }
        
    }];
    
    /*
    [[HttpRequest  currentHttpRequest] postPath:MainIndex_API params:nil resultBlock:^(NSDictionary *Data, MsgModel *msgModel)
    {
        NSLog(@"Data--%@",Data);
        if (msgModel.status == 200)
        {
            NSArray *banners = Data[@"banners"];
            NSArray *notices = Data[@"notices"];
            NSArray *recommendItems = Data[@"recommendItems"];
           
            self.salesBoard = Data[@"salesBoard"];
            self.todoList = Data[@"todoList"];
            NSLog(@"salesBoard--%@",self.salesBoard);
            NSLog(@"todoList--%@",self.todoList);
            self.currentCapital = self.salesBoard[@"currentCapital"];
            self.dayStatistics = self.salesBoard[@"dayStatistics"];
            self.weekStatistics = self.salesBoard[@"weekStatistics"];
            self.monthStatistics = self.salesBoard[@"monthStatistics"];
            
            NSError *error;
            //1,banner
            for (NSDictionary *dic in banners)
            {
                BannersModel *model = [[BannersModel alloc] initWithDictionary:dic error:&error];
                [myself.bannerImageArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [myself addBanner];
            });
            
            //2,notices
            for (NSDictionary *dic in notices)
            {
                NoticesModel *model = [[NoticesModel alloc] initWithDictionary:dic error:&error];
                [myself.noticesArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [myself addNotices];
            });
            
            //3,cell数据
            for (NSDictionary *dic in recommendItems)
            {
                MainModel *model = [[MainModel alloc] initWithDictionary:dic error:&error];
                [myself.tableViewArray addObject:model];
            }
            
            //5,停止刷新
            [myself.myTableView.mj_header endRefreshing];
            myself.myTableView.tableHeaderView = [self createHeaderView];
            [myself.myTableView reloadData];
        }
        else {
            [myself.myTableView.mj_header endRefreshing];
        }
    }];
     */
}

#pragma mark - 添加banner
- (void)addBanner
{
    [self.pageFlowView removeFromSuperview];
    
    self.pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 170)];
    self.pageFlowView.backgroundColor = [UIColor whiteColor];
    self.pageFlowView.delegate = self;
    self.pageFlowView.dataSource = self;
    self.pageFlowView.minimumPageAlpha = 0.1;
    self.pageFlowView.isCarousel = YES;
    self.pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    self.pageFlowView.isOpenAutoScroll = YES;
    self.pageFlowView.autoTime = 3.0f;
    [self.pageFlowView reloadData];
    [self.bgBannerView addSubview:self.pageFlowView];
}

#pragma mark - 添加公告
- (void)addNotices
{
    [self.bgNoticeView addSubview:self.verticalMarquee];
    [self.verticalMarquee scrollWithCallbackBlock:^(JhtVerticalMarquee *view, NSInteger currentIndex)
    {
        //NSLog(@"滚动到第 %ld 条数据", (long)currentIndex);
    }];
    
    NSMutableArray *titltArray = [NSMutableArray array];
    for (NoticesModel *model in self.noticesArray)
    {
        [titltArray addObject:model.title];
    }
    
    self.verticalMarquee.sourceArray = titltArray;
    // 开始滚动
    [self.verticalMarquee marqueeOfSettingWithState:MarqueeStart_V];
}

/** 纵向 跑马灯 */
- (JhtVerticalMarquee *)verticalMarquee
{
    if (!_verticalMarquee)
    {
        _verticalMarquee = [[JhtVerticalMarquee alloc]  initWithFrame:CGRectMake(15, 20, SCREENWIDTH-62-45, 36)];
        _verticalMarquee.tag = 101;
        //        _verticalMarquee.isCounterclockwise = YES;
        _verticalMarquee.verticalNumberOfLines = 0;
//        _verticalMarquee.backgroundColor = [UIColor yellowColor];
        _verticalMarquee.verticalTextColor = RGBACOLOR(255, 219, 102, 1);
        // 添加点击手势
        //UITapGestureRecognizer *vtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(marqueeTapGes:)];
        //[_verticalMarquee addGestureRecognizer:vtap];
    }
    
    return _verticalMarquee;
}

#pragma mark - 创建表视图
- (void)createTableView
{
    if (@available(iOS 11.0, *))
    {
        self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, SCREENWIDTH, SCREENHEIGHT-60-TabBarHeight-BottomHeight_xs) style:UITableViewStyleGrouped];
    }
    else {
        self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, SCREENWIDTH, SCREENHEIGHT-60-TabBarHeight) style:UITableViewStyleGrouped];
    }
    
    [self.view addSubview:self.myTableView];
    self.myTableView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.sectionHeaderHeight = 10.f;
    self.myTableView.sectionFooterHeight = 0.01f;
    self.myTableView.tableHeaderView = [self createHeaderView];
    self.myTableView.tableFooterView = [self tableViewFoot];
    [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.myTableView withView:self];
    [self.myTableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"MainTableViewCell"];
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(httpRequestData)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.tableViewArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 475.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.shanrkBtn.indexPath = indexPath;
    [cell.shanrkBtn addTarget:self action:@selector(shanrkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    MainModel *model = self.tableViewArray[indexPath.section];
    [cell setCellModel:model];
    
    return cell;
}

- (UIView *)tableViewFoot
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 0.01)];
    return view;
}

#pragma mark - 创建表头
- (UIView *)createHeaderView
{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = RGBACOLOR(249, 249, 249, 1);
    //1，轮播图
    [self createBannerView:bgView];
    //2，公告
    [self createNoticeView:bgView];
    //3，创建销售看板
    [self createSellLookView:bgView];
    //4，创建待办事项
    [self createBacklogView:bgView];
    //5，创建今日推荐
    [self createRecommendView:bgView];
    
    bgView.frame = CGRectMake(0, 0, SCREENWIDTH, self.recommendBgView.bottom);
    return bgView;
}

#pragma mark - 创建轮播图
- (void)createBannerView:(UIView *)bgView
{
    self.bgBannerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 170)];
    self.bgBannerView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:self.bgBannerView];
}

#pragma mark - 创建公告模板
- (void)createNoticeView:(UIView *)bgView
{
    self.bgNoticeView = [[UIView alloc]init];
    self.bgNoticeView.frame = CGRectMake(0, self.bgBannerView.bottom, SCREENWIDTH, 56);
    self.bgNoticeView.backgroundColor = RGBACOLOR(249, 249, 249, 1);
//    self.bgNoticeView.backgroundColor = [UIColor redColor];
    [bgView addSubview:self.bgNoticeView];
    
    UIView *topView = [[UIView alloc] init];
    [self.bgNoticeView addSubview:topView];
    topView.frame = CGRectMake(0, 0, SCREENWIDTH, 20);
    topView.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 销售看板模块
- (void)createSellLookView:(UIView *)bgView
{
    self.sellLookBgView = [[UIView alloc] init];
    [bgView addSubview:self.sellLookBgView];
    self.sellLookBgView.frame = CGRectMake(0, self.bgNoticeView.bottom, SCREENWIDTH, 187);
    self.sellLookBgView.backgroundColor = [UIColor whiteColor];
    
    UILabel *sellLabel = [[UILabel alloc] init];
    [self.sellLookBgView addSubview:sellLabel];
    sellLabel.frame = CGRectMake(15, 30, 80, 18);
    sellLabel.text = @"销售看板";
    sellLabel.textAlignment = 0;
    sellLabel.textColor = BlackTextColor;
    sellLabel.font = FontWithSize(18);
    
    NSString *text = [NSString stringWithFormat:@"当前余额  %@元",self.currentCapital];
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f]}];
    UILabel *moneyLabel = [[UILabel alloc] init];
    [self.sellLookBgView addSubview:moneyLabel];
    moneyLabel.frame = CGRectMake(SCREENWIDTH- size.width-20, 33, size.width+10, 12);
    moneyLabel.text = text;
    moneyLabel.textAlignment = 0;
    moneyLabel.textColor = BlackTextColor;
    moneyLabel.font = FontWithSize(14);
    
    UIImageView *moneyIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"balance"]];
    [self.sellLookBgView addSubview:moneyIcon];
    moneyIcon.frame = CGRectMake(moneyLabel.left-6-12, 33, 10, 12);
    
    CGFloat scrollWidth =self.sellLookBgView.height-15-(moneyLabel.bottom+20);
    UIScrollView *myScrollView = [[UIScrollView alloc] init];
    [self.sellLookBgView addSubview:myScrollView];
    myScrollView.frame = CGRectMake(0, sellLabel.bottom+20, SCREENWIDTH, scrollWidth);
    myScrollView.contentSize = CGSizeMake(SCREENWIDTH*3, scrollWidth);
    myScrollView.pagingEnabled = YES;
    
    for (int i = 0; i < 3; i++)
    {
        UIView *view = [[UIView alloc]init];
        [myScrollView addSubview:view];
        myScrollView.clipsToBounds = NO;
        view.frame = CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, scrollWidth);
        
        switch (i) {
            case 0:
            {
//                view.backgroundColor = [UIColor orangeColor];
                [self createView:view dic:self.dayStatistics offset:i];
            }
                break;
                
            case 1:
            {
//                view.backgroundColor = [UIColor redColor];
                [self createView:view dic:self.weekStatistics offset:i];
            }
                break;
                
            case 2:
            {
//                view.backgroundColor = [UIColor yellowColor];
                [self createView:view dic:self.monthStatistics offset:i];
            }
                break;
                
            default:
                break;
        }
    }   
}

- (UIView *)createView:(UIView *)view dic:(NSDictionary *)dic offset:(int)offset
{
    view.clipsToBounds = NO;
    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sale_bg_1"]];
    [view addSubview:bgImage];
    bgImage.frame = CGRectMake(16, 4, view.width-24, view.height);
//    bgImage.backgroundColor = [UIColor orangeColor];
    
    UIView *bgView = [[UIView alloc] init];
    [view addSubview:bgView];
    bgView.frame = CGRectMake(12, 0, view.width-24, view.height);
    bgView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *dataBgImageView = [[UIImageView alloc] init];
    [bgView addSubview:dataBgImageView];
    dataBgImageView.frame = CGRectMake(0, 0, bgView.width, bgView.height);
    dataBgImageView.image = [UIImage imageNamed:@"sale_bg"];
    
    for (int i = 0; i < 3; i++)
    {
        CGFloat width = dataBgImageView.width/3;
        CGFloat height = dataBgImageView.height;
        [self sellLookView:dataBgImageView frame:CGRectMake(width*(i%3), 0, width, height) index:i offset:offset dic:dic];
    }
    
    return bgView;
}

- (UIView *)sellLookView:(UIView *)bgView
                   frame:(CGRect)frame
                   index:(int)index
                  offset:(int)offset
                     dic:(NSDictionary *)dic
{
    UIView *view = [[UIView alloc] init];
    [bgView addSubview:view];
    view.frame = frame;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [view addSubview:titleLabel];
    titleLabel.frame = CGRectMake(10, 15, view.width-20, 12);
    titleLabel.textColor = BlackTextColor;
    titleLabel.font = FontWithSize(12);
    titleLabel.textAlignment = 1;
    
    UILabel *numberLabel = [[UILabel alloc] init];
    [view addSubview:numberLabel];
    numberLabel.frame = CGRectMake(10, titleLabel.bottom+10, view.width-20, 12);
    numberLabel.textAlignment = 1;
    numberLabel.textColor = RGBACOLOR(239, 208, 83, 1);
    numberLabel.font = FontWithSize(12);
    
    UILabel *borderLabel = [[UILabel alloc] init];
    [view addSubview:borderLabel];
    borderLabel.frame = CGRectMake(10, numberLabel.bottom+10, view.width-20, 10);
    borderLabel.textAlignment = 1;
    borderLabel.textColor = RGBACOLOR(194, 194, 194, 1);
    borderLabel.font = FontWithSize(10);
    
    UILabel *meLabel = [[UILabel alloc] init];
    [view addSubview:meLabel];
    meLabel.frame = CGRectMake(10, borderLabel.bottom+10, view.width-20, 10);
    meLabel.textAlignment = 1;
    meLabel.textColor = RGBACOLOR(194, 194, 194, 1);
    meLabel.font = FontWithSize(10);
    
    if (offset == 0)
    {//每日
        switch (index)
        {
            case 0:
            {
                titleLabel.text = @"今日订单数(笔)";
                numberLabel.text = [NSString stringWithFormat:@"%d",[(NSString *)dic[@"totalOrderCount"] intValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %d",[(NSString *)dic[@"platformOrderCount"] intValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %d",[(NSString *)dic[@"shopOrderCount"] intValue]];
            }
                break;
                
            case 1:
            {
                titleLabel.text = @"今日销售额(元)";
                numberLabel.text = [NSString stringWithFormat:@"%0.2f",[(NSString *)dic[@"totalSaleAmount"] floatValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %0.2f",[(NSString *)dic[@"platformSaleAmount"] floatValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %0.2f",[(NSString *)dic[@"shopSaleAmount"] floatValue]];
            }
                break;
                
            case 2:
            {
                titleLabel.text = @"今日收益(元)";
                numberLabel.text = [NSString stringWithFormat:@"%0.2f",[(NSString *)dic[@"totalGainsAmount"] floatValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %0.2f",[(NSString *)dic[@"platformGainsAmount"] floatValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %0.2f",[(NSString *)dic[@"shopGainsAmount"]floatValue]];
            }
                break;
                
            default:
                break;
        }
    }
    else if (offset == 1)
    {//每周
        switch (index)
        {
            case 0:
            {
                titleLabel.text = @"本周订单数(笔)";
                numberLabel.text = [NSString stringWithFormat:@"%d",[(NSString *)dic[@"totalOrderCount"] intValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %d",[(NSString *)dic[@"platformOrderCount"] intValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %d",[(NSString *)dic[@"shopOrderCount"] intValue]];
            }
                break;
                
            case 1:
            {
                titleLabel.text = @"本周销售额(元)";
                numberLabel.text = [NSString stringWithFormat:@"%0.2f",[(NSString *)dic[@"totalSaleAmount"] floatValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %0.2f",[(NSString *)dic[@"platformSaleAmount"] floatValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %0.2f",[(NSString *)dic[@"shopSaleAmount"] floatValue]];
            }
                break;
                
            case 2:
            {
                titleLabel.text = @"本周收益(元)";
                numberLabel.text = [NSString stringWithFormat:@"%0.2f",[(NSString *)dic[@"totalGainsAmount"] floatValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %0.2f",[(NSString *)dic[@"platformGainsAmount"] floatValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %0.2f",[(NSString *)dic[@"shopGainsAmount"]floatValue]];
            }
                break;
                
            default:
                break;
        }
    }
    else if (offset == 2)
    {//每月
        switch (index)
        {
            case 0:
            {
                titleLabel.text = @"本月订单数(笔)";
                numberLabel.text = [NSString stringWithFormat:@"%d",[(NSString *)dic[@"totalOrderCount"] intValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %d",[(NSString *)dic[@"platformOrderCount"] intValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %d",[(NSString *)dic[@"shopOrderCount"] intValue]];
            }
                break;
                
            case 1:
            {
                titleLabel.text = @"本月销售额(元)";
                numberLabel.text = [NSString stringWithFormat:@"%0.2f",[(NSString *)dic[@"totalSaleAmount"]floatValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %0.2f",[(NSString *)dic[@"platformSaleAmount"] floatValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %0.2f",[(NSString *)dic[@"shopSaleAmount"] floatValue]];
            }
                break;
                
            case 2:
            {
                titleLabel.text = @"本月收益(元)";
                numberLabel.text = [NSString stringWithFormat:@"%0.2f",[(NSString *)dic[@"totalGainsAmount"] floatValue]];
                borderLabel.text = [NSString stringWithFormat:@"跨境商品 %0.2f",[(NSString *)dic[@"platformGainsAmount"] floatValue]];
                meLabel.text = [NSString stringWithFormat:@"自营商品 %0.2f",[(NSString *)dic[@"shopGainsAmount"]floatValue]];
            }
                break;
                
            default:
                break;
        }
    }
    
    return view;
}

#pragma mark - 待办事项模板
- (void)createBacklogView:(UIView *)bgView
{
    self.backlogBgView = [[UIView alloc] init];
    [bgView addSubview:self.backlogBgView];
    self.backlogBgView.backgroundColor = [UIColor whiteColor];
    self.backlogBgView.frame = CGRectMake(0, self.sellLookBgView.bottom+10, SCREENWIDTH, 167);
    
    UIImageView *titleIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backlog_main"]];
    [self.backlogBgView addSubview:titleIcon];
    titleIcon.frame = CGRectMake(15, 20, self.backlogBgView.width-30, 60);
    
    UIImageView *dateIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"date_main"]];
    [titleIcon addSubview:dateIcon];
    dateIcon.frame = CGRectMake(20, 10, 40, 38);
    
    UILabel *titlLabel = [[UILabel alloc] init];
    [titleIcon addSubview:titlLabel];
    titlLabel.frame = CGRectMake(dateIcon.right+18, 21, 75, 18);
    titlLabel.text = @"待办事项";
    titlLabel.textAlignment = 0;
    titlLabel.textColor = BlackTextColor;
    titlLabel.font = FontMediumWithSize(18);
    
    UILabel *numberLable = [[UILabel alloc] init];
    [titleIcon addSubview:numberLable];
    numberLable.frame = CGRectMake(titleIcon.width-60-40, 21, 60, 18);
    numberLable.textAlignment = 2;
    numberLable.text = [NSString stringWithFormat:@"共%ld项",self.todoList.count];
    numberLable.font = FontWithSize(14);
    numberLable.textColor = BlackTextColor;

    if (self.todoList.count > 0)
    {
        CGFloat width = SCREENWIDTH-40;
        UIButton *todoBgView;
        for (int i = 0; i < self.todoList.count; i++)
        {
            #pragma mark - 有数据是待办事项的底部展示
            NSDictionary *dic = self.todoList[i];
            todoBgView = [UIButton buttonWithType:UIButtonTypeCustom] ;
            [self.backlogBgView addSubview:todoBgView];
            todoBgView.frame = CGRectMake(20+(40)*(i%1), titleIcon.bottom+10+(40+10)*(i/1), width, 40);
            todoBgView.layer.borderWidth = 1;
            todoBgView.layer.borderColor = RGBACOLOR(0, 0, 0, 0.1).CGColor;
            NSInteger type = [(NSString *)dic[@"type"] integerValue];
            todoBgView.tag = type;
            [todoBgView addTarget:self action:@selector(todoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            UIView *dian = [[UIView alloc] init];
            [todoBgView addSubview:dian];
            dian.frame = CGRectMake(10, 17, 5, 5);
            dian.backgroundColor = BtnBgColor;
            
            UILabel *label = [[UILabel alloc] init];
            [todoBgView addSubview:label];
            label.frame = CGRectMake(25, 0, todoBgView.width-25, 40);
            label.text = (NSString *)dic[@"message"];
            label.font = FontWithSize(12);
            label.textAlignment = 0;
            label.textColor = RGBACOLOR(153, 153, 153, 1);
            
            UIImageView *jiantou = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopMore"]];
            [todoBgView addSubview:jiantou];
            jiantou.frame = CGRectMake(SCREENWIDTH-14-40, 13, 8, 14);
        }
        self.backlogBgView.frame = CGRectMake(0, self.sellLookBgView.bottom+10, SCREENWIDTH, todoBgView.bottom+15);
    }
    else {
        #pragma mark - 没有数据时的待办事项的底部
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noData_main"]];
        [self.backlogBgView addSubview:icon];
        icon.frame = CGRectMake((SCREENWIDTH-146)/2, 100, 54, 42);
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [self.backlogBgView addSubview:titleLabel];
        titleLabel.frame = CGRectMake(icon.right+20, 115, 80, 12);
        titleLabel.text = @"暂无待办事项";
        titleLabel.textColor = RGBACOLOR(239, 208, 83, 1);
        titleLabel.textAlignment = 0;
        titleLabel.font = FontWithSize(12);
        self.backlogBgView.frame = CGRectMake(0, self.sellLookBgView.bottom+10, SCREENWIDTH, 167);
    }
}

#pragma mark - 待办事情绑定事件
- (void)todoBtnClick:(UIButton *)sender
{
    switch (sender.tag)
    {
        case 10:
        {//余额不足提醒
//            [self.navigationController pushViewController:[UIViewController new] animated:YES];
            RechargeViewController *recharge = [RechargeViewController new];
            recharge.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:recharge animated:YES];
        }
            break;
            
        case 20:
        {//提货订单
            self.pickVC = [[PickUpViewController alloc]init];
            [[UIApplication sharedApplication].windows[0] addSubview:self.pickVC.view];
            self.pickVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
            
            //[self.navigationController pushViewController:[UIViewController new] animated:YES];
        }
            break;
            
        case 40:
        {//成本价变动
            USER_DEFAULTS_SAVE(@"1", @"fromMain");//1表示从首页t切换过来
            self.tabBarController.selectedIndex = 1;
        }
            break;
            
        case 60:
        {//待解冻
//            [self.navigationController pushViewController:[UIViewController new] animated:YES];
            OrderViewController *orderVC = [OrderViewController new];
            orderVC.hidesBottomBarWhenPushed = YES;
            orderVC.isFronze = YES;
            orderVC.buttionIdex = 1;
            [self.navigationController pushViewController:orderVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 创建今日推荐模板
- (void)createRecommendView:(UIView *)bgview
{
    self.recommendBgView = [[UIView alloc] init];
    [bgview addSubview:self.recommendBgView];
    self.recommendBgView.frame = CGRectMake(0, self.backlogBgView.bottom+10, SCREENWIDTH, 58);
    self.recommendBgView.backgroundColor = [UIColor whiteColor];
    
    UILabel *tltleLabel = [[UILabel alloc] init];
    [self.recommendBgView addSubview:tltleLabel];
    tltleLabel.frame = CGRectMake(15, 20, 80, 18);
    tltleLabel.text = @"今日推荐";
    tltleLabel.font = FontMediumWithSize(18);
    tltleLabel.textAlignment = 0;
    tltleLabel.textColor = BlackTextColor;
    
    UIView *line = [[UIView alloc] init];
    [self.recommendBgView addSubview:line];
    line.frame = CGRectMake(0, self.recommendBgView.height-1, SCREENWIDTH, 1);
    line.backgroundColor =RGBACOLOR(241, 241, 241, 1);
}

#pragma mark NewPagedFlowView Delegate(卡片轮播代理)
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView
{
    return CGSizeMake(SCREENWIDTH - 60, 170);
}

#pragma mark - banner点击到了第几张图片
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex
{
    NSLog(@"点击了第%ld张图",(long)subIndex + 1);
    BannersModel *model = self.bannerImageArray[subIndex];
    NSString *url = [NSString stringWithFormat:@"%@",model.clickUrl];
    NSLog(@"url=%@",url);
    if (IsEmptyStr(url))
    {// url为空
        return;
    }
    
    if ([url containsString:@"weapp://"])
    {// 包含了weapp:// 跳转小程序
        url = [url substringFromIndex:8];
        NSLog(@"url=%@",url);
        WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
        launchMiniProgramReq.userName = @"gh_0307bfa8b637";  //拉起的小程序的username
        launchMiniProgramReq.path = [NSString stringWithFormat:@"%@",url];//拉起小程序页面的可带参路径，不填默认拉起小程序首页
        launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; //拉起小程序的类型
        [WXApi sendReq:launchMiniProgramReq];
    }
    else if ([url containsString:@"yibao://recharge"])
    {//包含yibao://recharge 跳转充值页面
        RechargeViewController *recharge = [RechargeViewController new];
        recharge.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:recharge animated:YES];
    }
    else {//跳转webview
        WebViewController *web = [[WebViewController alloc] init];
        web.hidesBottomBarWhenPushed = YES;
        web.urlString = @"";
        web.urlString = model.clickUrl;
        NSLog(@"clickUrl--%@",model.clickUrl);
        [self.navigationController pushViewController:web animated:YES];
    }
}

#pragma mark - 滚动到了第几张图片
- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView
{
    //NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
}

#pragma mark - 设置图片的数量
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView
{
    return self.bannerImageArray.count;
}

#pragma mark - 设置banner图片
- (PGIndexBannerSubiew *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index
{
    PGIndexBannerSubiew *bannerView = [flowView dequeueReusableCell];
    if (!bannerView)
    {
        bannerView = [[PGIndexBannerSubiew alloc] init];
        bannerView.tag = index;
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
    }
    BannersModel *model = self.bannerImageArray[index];
    //NSLog(@"imageUrl--%@",model.imageUrl);
    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.imageUrl]] placeholderImage:[UIImage imageNamed:@"Placeholder400"]];
    return bannerView;
}

#pragma mark - 一键分享
- (void)shanrkBtnClick:(UIButton *)sender
{
    NSLog(@"一键分享");
    MainModel *model = self.tableViewArray[sender.indexPath.section];
    SharkShoppingVC *shopping = [[SharkShoppingVC alloc] init];
    shopping.product_id = model.product_id;
    shopping.hidesBottomBarWhenPushed = YES;
    shopping.isHide = NO;
    [self.navigationController pushViewController:shopping animated:YES];
}

@end
